package com.dxp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dxp.common.R;
import com.dxp.pojo.User;
import com.dxp.service.UserService;
import com.dxp.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    JavaMailSenderImpl mailSender;
    @Autowired
    RedisTemplate redisTemplate;
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession session){
        String phone = user.getPhone();
        if(StringUtils.isNotEmpty(phone)){
            SimpleMailMessage mailMessage = new SimpleMailMessage();
            mailMessage.setSubject("你好呀!欢迎来到瑞吉外卖");
            String  code = ValidateCodeUtils.generateValidateCode(6).toString();
            log.info("code:"+code);
            mailMessage.setText("【瑞吉外卖】您的验证码为:"+code+",此验证码用于登录瑞吉外卖平台。(5分钟内有效)");
            mailMessage.setTo(phone);
            mailMessage.setFrom("493350232@qq.com");
            mailSender.send(mailMessage);
            //session.setAttribute(phone,code);
            //将验证码缓存到redis中,设置有效期5分钟
            redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
            return R.success("邮箱验证码发送成功");
        }
        return R.error("邮箱验证码发送失败");
    }
    @PostMapping("/login")
    public R<User> login(@RequestBody Map map, HttpSession session){
        String phone = map.get("phone").toString();
        String code = map.get("code").toString();
        log.info("phone: " + phone);
        log.info("code: " +code);
        //从redis中拿到验证码
        Object  codemsg= redisTemplate.opsForValue().get(phone);
        if(codemsg!=null&&codemsg.equals(code)){
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.eq("phone",phone);
            User user = userService.getOne(userQueryWrapper);
            if(user == null){
                //创建新用户
                user=new User();
                user.setPhone(phone);
                user.setStatus(1);
                userService.save(user);
            }
            session.setAttribute("user",user.getId());
            //用户登录成功,删除redis缓存中的验证码
            redisTemplate.delete(phone);
            return R.success(user);
        }
        return R.error("验证码错误");
    }
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("user");
        return R.success("注销成功");
    }
}
